package com.ggh.jinjilive.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.blankj.utilcode.util.LogUtils;
import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;

import java.util.ArrayList;
import java.util.List;

public class LocationUtils {
    //初始化定位
    private static AMapLocationClient locationClient;

    /**
     *  初始化定位
     */
    public static void positioning(AMapLocationListener aMapLocationListener){
        //初始化定位
        locationClient = new AMapLocationClient(BaseApplication.getInstance());
        //设置定位回调监听
        locationClient.setLocationListener(aMapLocationListener);
        //声明AMapLocationClientOption对象
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        /**
         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
         */
        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        //设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
        //设置定位模式为AMapLocationMode.Battery_Saving，低功耗模式。
        //设置定位模式为AMapLocationMode.Device_Sensors，仅设备模式。
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果：
        //该方法默认为false。
        mLocationOption.setOnceLocation(true);
        //获取最近3s内精度最高的一次定位结果：
        //设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
        mLocationOption.setOnceLocationLatest(true);
        //设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
        mLocationOption.setInterval(1000);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否允许模拟位置,默认为true，允许模拟位置
        mLocationOption.setMockEnable(true);
        //单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
        mLocationOption.setHttpTimeOut(20000);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(false);
        //给定位客户端对象设置定位参数
        locationClient.setLocationOption(mLocationOption);
    }

    /**
     * 启动定位
     */
    public static void startLocation(){
        if(null != locationClient){
            locationClient.startLocation();
        }
    }

    /**
     * //停止定位后，本地定位服务并不会被销毁
     */
    public static void stopLocation(){
        if(null != locationClient){
            locationClient.stopLocation();
        }
    }

    /**
     * //销毁定位客户端，同时销毁本地定位服务。
     */
    public static void onDestroy(){
        if(null != locationClient){
            locationClient.onDestroy();
        }
    }

    /**
     * 更新附近显示
     * @param tvNearMap
     * @param mLatLonPoint
     */
    public static void GeocodeSearch(final TextView tvNearMap, LatLonPoint mLatLonPoint){
        GeocodeSearch geocoderSearch = new GeocodeSearch(tvNearMap.getContext());
        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
                LogUtils.d(regeocodeResult.getRegeocodeAddress().getCity());
                tvNearMap.setText(regeocodeResult.getRegeocodeAddress().getCity());
            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
            }
        });
        RegeocodeQuery query = new RegeocodeQuery(mLatLonPoint, 0, GeocodeSearch.AMAP);
        geocoderSearch.getFromLocationAsyn(query);
    }

    /**
     * 更新附近显示
     * @param mLatLonPoint
     */
    public static void GeocodeSearch(LatLonPoint mLatLonPoint, AddOnClickListener1 addOnClickListener1){
        GeocodeSearch geocoderSearch = new GeocodeSearch(BaseApplication.getInstance());
        geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
                LogUtils.d(regeocodeResult.getRegeocodeAddress().getCity());
                addOnClickListener1.onSkipClicked(regeocodeResult);
            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
            }
        });
        RegeocodeQuery query = new RegeocodeQuery(mLatLonPoint, 0, GeocodeSearch.AMAP);
        geocoderSearch.getFromLocationAsyn(query);
    }





    /**
     * 根据经纬度获取附近信息
     * @param mContext
     * @param latLonPoint
     * @param addItemOnClickListener
     */
    public static void accessoryPositioningMessage(Context mContext, LatLonPoint latLonPoint, AddItemOnClickListener addItemOnClickListener){
        PoiSearch.Query query = new PoiSearch.Query("","");
        query.setPageSize(50);// 设置每页最多返回多少条poiitem
        query.setPageNum(0);//设置查询页码
        PoiSearch poiSearch = new PoiSearch(mContext, query);
        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latLonPoint.getLatitude(), latLonPoint.getLongitude()), 1000));//设置周边搜索的中心点以及半径
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                List<PoiItem> poiItemList = new ArrayList<>();
                poiItemList.addAll(poiResult.getPois());
                if(null != addItemOnClickListener){
                    addItemOnClickListener.onSkipClicked(0,poiItemList);
                }
            }

            @Override
            public void onPoiItemSearched(PoiItem poiItem, int i) {

            }
        });
        poiSearch.searchPOIAsyn();
    }



}
